// 碰撞系统
class CollisionSystem {
    constructor(world) {
        this.world = world;
    }

    // 更新系统
    update(deltaTime) {
        const tanks = this.world.getTanks();
        const bullets = this.world.getBullets();
        const walls = this.world.getWalls();
        const mapWidth = this.world.getMapWidth();
        const mapHeight = this.world.getMapHeight();

        // 检查子弹是否超出边界
        for (const bulletId in bullets) {
            const bullet = bullets[bulletId];
            if (bullet.isOutOfBounds(mapWidth, mapHeight)) {
                this.world.removeBullet(bulletId);
            }
        }

        // 检查子弹与墙壁的碰撞
        for (const bulletId in bullets) {
            const bullet = bullets[bulletId];
            if (!bullet.active) continue;

            let wallCollision = false;
            for (const wallId in walls) {
                const wall = walls[wallId];
                if (bullet.checkCollision(wall)) {
                    // 对墙壁造成伤害
                    wall.takeDamage(bullet.damage);
                    
                    // 如果墙壁被摧毁，从游戏世界中移除
                    if (wall.health <= 0) {
                        delete walls[wallId];
                    }
                    
                    // 移除子弹
                    this.world.removeBullet(bulletId);
                    wallCollision = true;
                    break;
                }
            }

            if (wallCollision) continue;
            
            // 检查子弹与其他子弹的碰撞
            let bulletCollision = false;
            for (const otherBulletId in bullets) {
                // 跳过自己
                if (otherBulletId === bulletId) continue;
                // 跳过已经失效的子弹
                const otherBullet = bullets[otherBulletId];
                if (!otherBullet.active) continue;
                
                if (bullet.checkCollision(otherBullet)) {
                    // 两颗子弹都被销毁
                    this.world.removeBullet(bulletId);
                    this.world.removeBullet(otherBulletId);
                    bulletCollision = true;
                    break;
                }
            }
            
            if (bulletCollision) continue;

            // 检查子弹与坦克的碰撞
            for (const tankId in tanks) {
                const tank = tanks[tankId];
                if (!tank.active) continue;

                // 子弹不能打中发射它的坦克
                if (tank.id === bullet.tankId) continue;

                if (bullet.checkCollision(tank)) {
                    // 处理坦克受到伤害
                    tank.takeDamage(bullet.damage);
                    
                    // 移除子弹
                    this.world.removeBullet(bulletId);

                    // 如果坦克被摧毁，触发坦克销毁事件
                    if (tank.health <= 0) {
                        this.world.removeTank(tankId);
                        this.world.emit('tankDestroyed', { tankId, playerId: tank.playerId });
                    }

                    break;
                }
            }
        }
    }
}

// 如果在浏览器环境中，将CollisionSystem添加到window对象
if (typeof window !== 'undefined') {
    window.CollisionSystem = CollisionSystem;
}

// 如果在Node.js环境中，导出CollisionSystem类
if (typeof module !== 'undefined' && module.exports) {
    module.exports = CollisionSystem;
}